<template>
  <div>
    <el-card class="box-card" shadow="never">
      <div slot="header" class="clearfix">
        <span class="card-title">{{ userId ? "编辑" : "新增" }}</span>
        <el-button
          style="float: right"
          size="mini"
          type="primary"
          @click="() => handleSubmit()"
          >保存</el-button
        >
      </div>
      <div class="text item">
        <el-form
          :model="user"
          :rules="rules"
          ref="userForm"
          label-width="80px"
          style="width: 70%"
        >
          <el-form-item :label="$t('common.name')" prop="userName">
            <el-input v-model="user.userName"></el-input>
          </el-form-item>
          <el-form-item :label="$t('common.email')" prop="email">
            <el-input v-model="user.email"></el-input>
          </el-form-item>
          <el-form-item :label="$t('common.phone')" prop="phoneNumber">
            <el-input v-model="user.phoneNumber"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" v-if="!this.id">
            <el-input type="password" v-model="user.password"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>
<script>
import { getUser, getUserRoles, addUser, updateUser } from "@/api/user";
export default {
  components: {},
  data() {
    return {
      user: {
        userName: "",
        email: "",
        phoneNumber: "",
        roleNames: [],
        roleIds: [],
        password: "",
        extraProperties: {
          UserType: 2,
        },
      },
      rules: {
        userName: [
          {
            required: true,
            message: this.$t("tips.requireItem"),
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: this.$t("tips.requireItem"),
            trigger: "blur",
          },
        ],
        email: [
          {
            required: true,
            message: this.$t("tips.requireItem"),
            trigger: "blur",
          },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    getUserData() {
      if (!this.userId) {
        return;
      }
      getUser(this.userId).then((res) => {
        this.user = res || {};
        this.user.roleIds = [];
      });
    },
    handleSubmit() {
      this.$refs["userForm"].validate((valid) => {
        if (valid) {
          this.saveData();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    saveData() {
      if (!this.userId) {
        addUser(this.user).then((res) => {
          this.$notify({
            title: "成功",
            message: "保存成功",
            type: "success",
            position: "bottom-right",
          });
          this.$emit("success", res);
        });
      } else {
        updateUser(this.user).then((res) => {
          this.$notify({
            title: "成功",
            message: "保存成功",
            type: "success",
            position: "bottom-right",
          });
          this.$emit("success", res);
        });
      }
    },
    handleCancel() {
      this.$emit("close");
    },
  },
  created() {
    this.getUserData();
  },
  mounted() {},
  computed: {
    userId() {
      return this.id || "";
    },
  },
  props: {
    id: {
      type: String,
      default: null,
    },
  },
};
</script>
<style scoped>
.dialog-footer {
  text-align: right;
}
</style>
